package com.axzhengxin.platform.afterloan.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.axzhengxin.platform.afterloan.domain.FspBizApplyInfo;
import com.axzhengxin.platform.afterloan.domain.FspBizApproveInfo;
import com.axzhengxin.platform.afterloan.domain.FspBizLoanInfo;
import com.axzhengxin.platform.afterloan.mapper.FspBizInfoMapper;
import com.axzhengxin.platform.afterloan.service.IAfterLoanBizInfoService;
import com.axzhengxin.platform.business.domain.BizApproveDetail;
import com.axzhengxin.platform.business.mapper.BizApproveDetailMapper;
import com.axzhengxin.platform.common.constants.DateFormatPatterns;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * AfterLoanBizInfoServiceImpl
 *
 * @author jiangnan
 * @date 2021/3/2
 */
@Slf4j
@Service
public class AfterLoanBizInfoServiceImpl implements IAfterLoanBizInfoService {

    private FspBizInfoMapper fspBizInfoMapper;
    @Autowired
    private BizApproveDetailMapper bizApproveDetailMapper;

    public AfterLoanBizInfoServiceImpl(FspBizInfoMapper fspBizInfoMapper) {
        this.fspBizInfoMapper = fspBizInfoMapper;
    }

    @DataScope(deptAlias = "d", userAlias = "u")
    @Override
    public List<FspBizApplyInfo> selectBizApplyInfoList(FspBizApplyInfo bizApplyInfo) {
        return fspBizInfoMapper.selectFspBizApplyInfoList(bizApplyInfo);
    }

    @DataScope(deptAlias = "d", userAlias = "u")
    @Override
    public List<FspBizApproveInfo> selectBizApproveInfoList(FspBizApproveInfo bizApproveInfo) {
        Date today = new Date();
        return fspBizInfoMapper.selectFspBizApproveInfoList(bizApproveInfo)
                .stream()
                // 判断授信是否过期
                .peek(approveInfo -> judgeExpiredApprove(approveInfo, today))
                .collect(Collectors.toList());
    }

    @DataScope(deptAlias = "d", userAlias = "u")
    @Override
    public List<FspBizApproveInfo> selectAllBizApproveInfoList(FspBizApproveInfo bizApproveInfo) {
        Date today = new Date();
        List<FspBizApproveInfo> result = fspBizInfoMapper.selectAllBizApproveInfoList(bizApproveInfo);
        for (FspBizApproveInfo approveInfo:result) {
            judgeExpiredApprove(approveInfo, today);
        }
        return result;
    }

    @DataScope(deptAlias = "d", userAlias = "u")
    @Override
    public List<FspBizApproveInfo> selectBizApproveInfoUnexpiredList(FspBizApproveInfo bizApproveInfo) {
        String todayStr = DateFormatUtils.format(new Date(), "yyyy/MM/dd");
        // 添加一个格式化过的“本日日期”参数，用于在查询时获取未到期的授信
        bizApproveInfo.getParams().put("today", todayStr);
        List<FspBizApproveInfo> result = fspBizInfoMapper.selectBizApproveInfoUnexpiredList(bizApproveInfo);
        for (FspBizApproveInfo approveInfo:result) {
            approveInfo.setExpired(Boolean.FALSE);
        }
        return result;
    }


    @DataScope(deptAlias = "d", userAlias = "u")
    @Override
    public List<FspBizLoanInfo> selectBizLoanInfoList(FspBizLoanInfo bizLoanInfo) {
        return fspBizInfoMapper.selectFspBizLoanInfoList(bizLoanInfo)
                .stream()
                // 按放款编号字符串排序
                .sorted(Comparator.comparing(FspBizLoanInfo::getId))
                .collect(Collectors.toList());
    }

    @DataScope(deptAlias = "d", userAlias = "u")
    @Override
    public List<FspBizLoanInfo> selectAllFspBizLoanInfoList(FspBizLoanInfo fspBizLoanInfo) {
        List<FspBizLoanInfo> result = fspBizInfoMapper.selectAllFspBizLoanInfoList(fspBizLoanInfo);
        for (FspBizLoanInfo loan:result) {
            loan.setSettled(NumberUtil.equals(loan.getBalance(), BigDecimal.ZERO));
        }
        return result;
    }

    @DataScope(deptAlias = "d", userAlias = "u")
    @Override
    public List<FspBizLoanInfo> selectUnfinishedFspBizLoanInfoList(FspBizLoanInfo fspBizLoanInfo) {
        List<FspBizLoanInfo> result = fspBizInfoMapper.selectUnfinishedFspBizLoanInfoList(fspBizLoanInfo);
        for (FspBizLoanInfo loan:result) {
            loan.setSettled(Boolean.FALSE);
        }
        return result;
    }

    @Override
    public List<BizApproveDetail> selectBizApproveDetailList(List<String> list) {
        return bizApproveDetailMapper.selectDetailListByApproveIds(list);
    }

    /**
     * 判断授信是否到期
     *
     * @param bizApproveInfo 授信信息
     * @param today          今日日期
     */
    private void judgeExpiredApprove(FspBizApproveInfo bizApproveInfo, Date today) {
        Optional.of(bizApproveInfo)
                .filter(info -> StringUtils.isNotBlank(info.getApproveEnd()))
                .ifPresent(info -> {
                    try {
                        if (DateUtils.parseDate(info.getApproveEnd(), DateFormatPatterns.YYYY_MM_DD).before(today)) {
                            bizApproveInfo.setExpired(Boolean.TRUE);
                        } else {
                            bizApproveInfo.setExpired(Boolean.FALSE);
                        }
                    } catch (ParseException e) {
                        log.error("解析授信到期时间失败", e);
                    }
                });
    }

}
